﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Ebiz.libs;
using System.Data;
using System.Web.Services;
using System.Web.Script.Services;

namespace Ebiz
{
    public partial class MenuAccess : System.Web.UI.Page
    {
        public Dictionary<string, string> ViewData = new Dictionary<string,string>();
        protected void Page_Load(object sender, EventArgs e){
            string RoleID = Func.GetUserInfo(Func.ShowInfo._RoleID);
            //ViewData = LoadData(RoleID);
            for (int idx = 1; idx < 13; idx++) {
                ViewData.Add(idx.ToString(), "");
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static Dictionary<string, string> LoadData(string _RoleID) {
            Dictionary<string, string> result = new Dictionary<string, string>();
            string dbName = DBUtilities.GetDBName("devCon");
            string qry = "SELECT IDMenu FROM " + dbName + ".dbo.mstRole WHERE " +
                "(IDRole = @IDRole)";
            SqlConnection cn = new SqlConnection(DBUtilities.strConnection("devCon"));
            using (cn){
                try{
                    cn.Open();
                    
                    SqlCommand cmd = new SqlCommand(qry, cn);
                    cmd.Parameters.AddWithValue("@IDRole", _RoleID);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows) {
                        if (rdr.Read()) {
                            string[] tmpList = (rdr.GetValue(0).ToString()).Split(',');
                            for (int idx = 1; idx < 13; idx++) {
                                string isChecked = "";
                                foreach (string mnuCode in tmpList){
                                    if (idx == Convert.ToInt32(mnuCode)){
                                        isChecked = "checked='checked'";
                                        break;
                                    }else{
                                        isChecked = "";
                                    }
                                }
                                result.Add(idx.ToString(), isChecked);
                            }
                        }
                    }
                }catch (Exception ex){
                    string errMsg = ex.Message;
                }finally{
                    if (cn.State == ConnectionState.Open){
                        cn.Close();
                    }
                }
            }
            return result;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<object> UpdateMenu(string Role, List<string> ActiveMenu) {
            List<object> result = new List<object>();
            if (!(Role == "0")){
                if(Role == "1" || Role == "2"){
                    result.Add(0);
                    result.Add("Changes for this type of Role is not available!");
                }else{
                    string IDMenu = string.Join(",", ActiveMenu.ToArray());
                    string dbName = DBUtilities.GetDBName("devCon");
                    string qry = "UPDATE " + dbName + ".dbo.mstRole SET IDMenu = @IDMenu WHERE " +
                        "(IDRole = @IDRole)";
                    SqlConnection cn = new SqlConnection(DBUtilities.strConnection("devCon"));
                    using (cn){
                        try{
                            cn.Open();
                            SqlCommand cmd = new SqlCommand(qry, cn);
                            cmd.Parameters.AddWithValue("@IDMenu", IDMenu);
                            cmd.Parameters.AddWithValue("@IDRole", Role);
                            if (cmd.ExecuteNonQuery() > 0){
                                result.Add(1);
                                result.Add("Update Data Success!");
                            }else{
                                result.Add(0);
                                result.Add("Update Data Failed!");
                            }
                        }catch (Exception ex){
                            string errMsg = ex.Message;
                        }finally{
                            if (cn.State == ConnectionState.Open){
                                cn.Close();
                            }
                        }
                    }
                }
            }else{
                result.Add(0);
                result.Add("Select Role First!");
            }
            return result;
        }
    }
}
